    using System;
using System.Collections.Generic;
using System.Text;

namespace modul1
{
    public class Character : Object
    {
        #region Properties
        private string _value;
        private int? _maxLength;
        #endregion

        #region Constructors
        public Character(string value)
        {
            _value = value;

            if (value != null)
                _maxLength = value.Length;
            else
                _maxLength = 0;
        }

        public Character(string value, int maxLenght)
        {
            _maxLength = maxLenght;
            if (value.Length > maxLenght)
                _value = value.Substring(0, maxLenght);
            else
            {
                _value = value;
                if (value != null)
                    _value =  _value.PadRight((int)_maxLength, ' ');
            }

            _maxLength = maxLenght;
        }

        public Character(int maxlength)
        {
            _maxLength = maxlength;
        }
        #endregion

        public Character(byte[] value, int maxlength)
        {
            _maxLength = maxlength;
            FromByte(value);
        }

        /// <summary>
        /// Converts current value to array of bytes.
        /// </summary>
        /// <returns></returns>
        #region Public properties
        public override byte[] ToByte()
        {
            if (!this.IsNull())
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] buffer = encoding.GetBytes(_value);
                byte[] bytes = new byte[buffer.Length + 1];
                bytes[0] = 1;
                for (int i = 1; i < bytes.Length; i++)
                {
                    bytes[i] = buffer[i - 1];
                }

                return bytes;
            }
            else
            {
                byte[] buffer = new byte[1];
                buffer[0] = 0;
                return buffer;
            }
        }

        /// <summary>
        /// Converts the array of bytes to Character value.
        /// </summary>
        /// <param name="buffer"></param>
        public override void FromByte(byte[] buffer)
        {
            if (buffer[0] == 1)
            {
                //if (buffer.Length < 2)
                //    throw new ApplicationException("Invalid array format.");
                ASCIIEncoding encoding = new ASCIIEncoding();
                _value = encoding.GetString(buffer, 1, buffer.Length - 1);
            }
            else if (buffer[0] == 0)
            {
                this._value = null;
            }
            else
                throw new ApplicationException("Invalid first byte format in array of bytes");

            if (_value != null)
            {
                if (_value.Length > _maxLength)
                {
                    _value = _value.Substring(0, (int)_maxLength);
                }
                else
                {
                    _value = _value.PadRight((int)_maxLength, ' ');
                }
            }
        }

        public override int Compare(modul1.Object val)
        {
            Character value = val as Character;
            if (value.IsNull() && this.IsNull())
                return 0;
            else if (value.IsNull() && !this.IsNull())
                return 1;
            else if (!value.IsNull() && this.IsNull())
                return -1;
            else 
                return String.Compare( value._value, this._value);
        }


        /// <summary>
        /// If current value is null then return True, else return False.
        /// </summary>
        /// <returns></returns>
        public override bool IsNull()
        {
            if (String.IsNullOrEmpty(_value))
            {
                if (_value != null)
                {
                    return false;
                }
                else
                    return true;
            }
            else
                return false;
        }

        /// <summary>
        /// If value is type of Character return True, else return False.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsTypeOf(Object value)
        {
            System.Type type = value.GetType();

            if (type.FullName == "Structures.Character2")
                return true;
            else
                return false;
        }

        /// <summary>
        /// If value can be parsed to Character type return true, else return false.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsTypeOf(string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                if (value != null)
                {
                    return true;
                }
                else
                    return false;
            }
            else
                return true; 
        }

        /// <summary>
        /// Get number of bytes needed to Character type.
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            if (_value != null)
                return _value.Length + 1;
            else
                return 1;
        }

        public override string ToString()
        {
            return _value;
        }

        #endregion
    }
}
